local skels = {}

local huayun = fk.CreateSkill{
    name = "ym1__huayun",
    attached_skill_name = "ym1__huayun_viewas&",
}
Fk:loadTranslationTable{
    ["ym1__huayun"] = "花韵",
    [":ym1__huayun"] = "一名角色可将红色和装备各一张牌当执行所有项的【酒】对你使用。若不为你，结算后其可依次视为对你使用【树上开花】和【顺手牵羊】。",
    ["#ym1__huayun"] = "花韵：将红色和装备各一张牌当执行所有项的【酒】使用，或不选牌询问其他角色是否如此做（须指定目标）",
    ["#ym1__huayun-viewas"] = "花韵：是否将红色和装备各一张牌当执行所有项的【酒】对%dest使用（须指定目标）",
    ["#ym1__huayun-use"] = "花韵：是否将红色和装备各一张牌当执行所有项的【酒】对目标使用（须指定目标）",
    ["#ym1__huayun-after"] = "花韵：是否依次视为对%dest使用【树上开花】和【顺手牵羊】",

    ["ym1__huayun_viewas&"] = "花韵",
    [":ym1__huayun_viewas&"] = "你可以将红色和装备各一张牌当执行所有项的【酒】对指定目标使用，然后可依次视为对其使用【树上开花】和【顺手牵羊】。",
}
huayun:addEffect("viewas",{
    pattern = "analeptic",
    prompt = "#ym1__huayun",
    card_filter = function (self, player, to_select, selected)
        local card = Fk:getCardById(to_select)
        if #selected == 0 then
            return card.color == Card.Red or card.type == Card.TypeEquip
        elseif #selected < 2 then
            local selectedCard = Fk:getCardById(selected[1])
            if selectedCard.color == Card.Red and card.type == Card.TypeEquip then return true end
            if selectedCard.type == Card.TypeEquip and card.color == Card.Red then return true end
        end
        return false
    end,
    view_as = function (self, player, cards)
        if #cards > 0 and #cards < 2 then return end
        local card = Fk:cloneCard("analeptic")
        card.skill = Fk.skills["huayun__analeptic_skill"]
        card.skillName = huayun.name
        if #cards == 2 then
            card:addSubcards(cards)
        end
        return card
    end,
    before_use = function (self, player, use)
        if #use.card.subcards == 0 then
            local room = player.room
            for _, tp in ipairs(room:getOtherPlayers(player)) do
                room:setPlayerMark(tp,"ym1__huayun",player.id)
                local bool ,dat = room:askToUseActiveSkill(tp,{
                    skill_name = "ym1__huayun_viewas&",
                    prompt = "#ym1__huayun-viewas::"..player.id
                })
                room:setPlayerMark(tp,"ym1__huayun",0)
                if bool and dat then
                    use.card:addSubcards(dat.cards)
                    use.from = tp
                else
                    return huayun.name
                end
            end
        end
    end,
    enabled_at_response = function (self, player, response)
        return not response
    end,
})
huayun:addEffect(fk.PreCardEffect,{
    can_refresh = function (self, event, target, player, data)
        return data.card.trueName == "analeptic" and data.card.skillName == huayun.name
    end,
    on_refresh = function (self, event, target, player, data)
        local card = data.card:clone()
        local c = table.simpleClone(data.card)
        for k, v in pairs(c) do
        card[k] = v
        end
        card.skill = Fk.skills["huayun__analeptic_skill"]
        data.card = card
    end,
})
huayun:addEffect(fk.CardUseFinished,{
    can_refresh = function (self, event, target, player, data)
        return data.from ~= player and data.from:isAlive() and table.contains(data.tos,player) and data.card.trueName == "analeptic" and data.card.skillName == huayun.name
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        if room:askToSkillInvoke(data.from,{
            skill_name = huayun.name,
            prompt = "#ym1__huayun-after::"..player.id,
        }) then
            for _, name in ipairs({"bogus_flower","snatch"}) do
                local card = Fk:cloneCard(name)
                card.skillName = huayun.name
                if not data.from:isProhibited(player, card) and (player:canUseTo(card,player,{bypass_distances = true}) or data.from:canUseTo(card,player,{bypass_distances = true})) then
                    room:useVirtualCard(name, nil, data.from, {player}, huayun.name)
                end
            end
        end
    end,
})
table.insert(skels,huayun)

local analeptic_skill = fk.CreateSkill{
    name = "huayun__analeptic_skill"
}
analeptic_skill:addEffect("cardskill",{
    mute = true,
    prompt = function(self, _, _, _, extra_data)
        return extra_data.analepticRecover and "#peach_skill" or "#analeptic_skill"
    end,
    max_turn_use_time = 1,
    mod_target_filter = function (self, player, to_select, selected, card, extra_data)
        return to_select:hasSkill(huayun.name)
    end,
    target_num = 1,
    target_filter = Util.CardTargetFilter,
    can_use = function(self, player, card, extra_data)
        return ((extra_data and (extra_data.bypass_times or extra_data.analepticRecover)) or
        self:withinTimesLimit(player, Player.HistoryTurn, card, "analeptic", player))
    end,
    on_use = function(self, room, use)
        if use.extra_data and use.extra_data.analepticRecover then
        use.extraUse = true
        end
    end,
    on_effect = function(self, room, effect)
        local to = effect.to
        if to:isWounded() and not to.dead then
            room:recover({
                who = to,
                num = 1,
                recoverBy = effect.from,
                card = effect.card,
            })
        end
        to.drank = to.drank + 1 + ((effect.extra_data or {}).additionalDrank or 0)
        room:broadcastProperty(to, "drank")
    end,
})
table.insert(skels,analeptic_skill)

local huayun_viewas = fk.CreateSkill{
    name = "ym1__huayun_viewas&",
}
huayun_viewas:addEffect("viewas",{
    card_num = 2,
    pattern = "analeptic",
    prompt = function (self, player, selected_cards, selected_targets)
        if player:getMark("ym1__huayun") ~= 0 then
            return "#ym1__huayun-viewas::"..player:getMark("ym1__huayun")
        else
            return "#ym1__huayun-use"
        end
    end,
    card_filter = function (self, player, to_select, selected)
        local card = Fk:getCardById(to_select)
        if #selected == 0 then
            return card.color == Card.Red or card.type == Card.TypeEquip
        elseif #selected < 2 then
            local selectedCard = Fk:getCardById(selected[1])
            if selectedCard.color == Card.Red and card.type == Card.TypeEquip then return true end
            if selectedCard.type == Card.TypeEquip and card.color == Card.Red then return true end
        end
        return false
    end,
    view_as = function (self, player, cards)
        if #cards ~= 2 then return end
        local card = Fk:cloneCard("analeptic")
        card.skill = Fk.skills["huayun__analeptic_skill"]
        card.skillName = huayun.name
        card:addSubcards(cards)
        return card
    end,
    enabled_at_response = function (self, player, response)
        return not response
    end,
    --[[feasible = function (self, player, selected, selected_cards, card)
        if #selected_cards ~= 2 then return false end
        local analeptic = Fk:cloneCard("analeptic")
        analeptic.skill = Fk.skills["huayun__analeptic_skill"]
        analeptic.skillName = huayun.name
        analeptic:addSubcards(selected_cards)
        return player:canUseTo(analeptic,player:getMark("ym1__huayun"))
    end,]]--
})
table.insert(skels,huayun_viewas)

return skels